package com.wjz.admin.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

/**
 * Method performance monitor
 */
@Slf4j
@Aspect
@Component
public class PerfMonitor {

    @Pointcut("execution(public * com.wjz.admin.*.*.*(..))")
    public void logControllerAspect(){}

    @Pointcut("execution(public * com.wjz.commons.*.*.*(..))")
    public void logServiceAspect(){}


    @Pointcut("logControllerAspect() || logServiceAspect()")
    public void logAspect(){
    }

    @Around("logAspect()")
    public Object deAround(ProceedingJoinPoint joinPoint) throws Throwable{
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        Object retVal = joinPoint.proceed();
        stopWatch.stop();
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        log.info("执行方法: {}.{}, Cost: {}", signature.getDeclaringTypeName(), signature.getName(), stopWatch.getTotalTimeMillis());

        return retVal;
    }

}
